package com.sui.bigdata.rtcadmin.controller;

import com.sui.bigdata.rtcadmin.model.vo.CommonResponse;
import com.sui.bigdata.rtcadmin.repository.model.CreateInfo;
import com.sui.bigdata.rtcadmin.service.MetaService;
import com.sui.bigdata.rtcadmin.util.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author YongChen
 * @date 2019/11/6 14:41
 * @description
 * @email yong_chen@sui.com
 */

@RestController
@CrossOrigin
@RequestMapping(value = "/meta")
@Slf4j
public class MetaController extends BaseController {
    @Autowired
    private MetaService metaService;


    @GetMapping("all")
    public CommonResponse all() {
        return CommonResponse.Builder.buildSuccess(metaService.queryAll());
    }

    @GetMapping("filter")
    public CommonResponse filter(@RequestParam("type")String type,@RequestParam("component")String component) {
        return CommonResponse.Builder.buildSuccess(metaService.queryByType(type,component));
    }

    @GetMapping
    public CommonResponse one(@RequestParam("tableName")String tableName){
        return CommonResponse.Builder.buildSuccess(metaService.queryByTableName(tableName));
    }

    @DeleteMapping
    public CommonResponse delete(@RequestParam("tableName")String tableName, @RequestParam("databaseName")String databaseName ) throws Exception{
        metaService.delete(tableName, databaseName);
        return CommonResponse.Builder.buildSuccess(null);
    }

    @PostMapping
    public CommonResponse add(@RequestBody CreateInfo createInfo) throws Exception{
        metaService.insert(createInfo);
        return CommonResponse.Builder.buildSuccess(null);
    }

    @GetMapping("list")
    public CommonResponse list(@RequestParam("type")String type) throws Exception{
        return CommonResponse.Builder.buildSuccess(metaService.list(type));
    }

    @GetMapping("function")
    public CommonResponse operation(@RequestParam("action")String action) throws Exception{
        return CommonResponse.Builder.buildSuccess(metaService.functionOperation(action));
    }

}
